package com.tao.power.Service.ServiceImpl;

import com.tao.power.Dao.MenusMapper;
import com.tao.power.Dto.MenuDto;
import com.tao.power.Dto.Result;
import com.tao.power.Enums.ErrorEnums;
import com.tao.power.Enums.MenuTypeEnums;
import com.tao.power.Pojo.Menu;
import com.tao.power.Service.MenuService;
import com.tao.power.Utils.verifyUtils;
import com.tao.power.config.AuthorityAnnotation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
@Slf4j
public class MenuServiceImpl implements MenuService {

    @Resource
    private MenusMapper menusMapper;
    private verifyUtils verifyUtils;


    @Override
    public Result queryMenus() {
        List<Menu> menus = new ArrayList<>();//返回菜单
        List<Menu> menuList = menusMapper.queryMenus();//全部菜单
        Map<Integer, Menu> menuMap = menuList.stream().collect(Collectors.toMap(Menu::getId, menu -> menu));
        for (Menu menu : menuList) {//遍历全部菜单
            if (!menu.getMenuType().equals("2")) {
                String parentId = menu.getSuperMenu();//获得父id
                if (parentId != null) {
                    Menu parent = menuMap.get(Integer.parseInt(parentId));
                    parent.getChildren().add(menu);
                    if (parent.getSuperMenu() == null) {
                        menus.add(parent);
                    }
                } else {
                    menus.add(menu);
                }
            }
        }
        List<Menu> list = menus.stream().distinct().collect(Collectors.toList());
        return Result.ok("", list, ErrorEnums.Success.getValue());
    }

    @Override
    public Result queryMenusList(Integer count, Integer page) {
        int index = count * (page - 1);
        List<Menu> menuList = menusMapper.queryMenusList(count, index);
        List<Menu> menus = menusMapper.queryMenus();
        return Result.ok("", menuList, (long) menusMapper.queryMenusListTotal(), ErrorEnums.Success.getValue());
    }


    @Override
    public Result deleteMenus(Integer id) {
        if (id == null) {
            return Result.fail(ErrorEnums.InfoIsEmpty.getLabel(), ErrorEnums.InfoIsEmpty.getValue());
        }
        int count = menusMapper.deleteMenus(id);
        if (count == 0) {
            return Result.fail(ErrorEnums.Deleteinfo_Fail.getLabel(), ErrorEnums.Deleteinfo_Fail.getValue());
        }
        return Result.ok("", ErrorEnums.Success.getValue());
    }

    @Override

    public Result updateMenus(Menu menu) {
        if (menu == null) {
            return Result.fail(ErrorEnums.InfoIsEmpty.getLabel(), ErrorEnums.InfoIsEmpty.getValue());
        }
        verifyUtils.menuVerify(menu);
        return getResult(menu);
    }


    @Override
    public Result insertMenus(Menu menu) {
        if (menu == null) {
            return Result.fail(ErrorEnums.InfoIsEmpty.getLabel(), ErrorEnums.InfoIsEmpty.getValue());
        }
        verifyUtils.menuVerify(menu);
        int count = menusMapper.insertMenus(menu);
        if (count == 0) {
            return Result.fail(ErrorEnums.AddInFO_Fail.getLabel(), ErrorEnums.AddInFO_Fail.getValue());
        }
        return Result.ok("", ErrorEnums.Success.getValue());
    }

    @Override
    public Result queryMenusBySearch(String menuName, String menuStatus, String menuType, Integer currentPage, Integer pageSize) {
        int count = pageSize * (currentPage - 1);
        List<Menu> menuList = menusMapper.queryMenusBySearch(menuName, menuStatus, menuType, count, pageSize);
        return Result.ok("", menuList, (long) menusMapper.queryMenusBySearchTotal(menuName, menuStatus, menuType),
                ErrorEnums.Success.getValue());
    }

    @Override
    public Result batchDel(List<Integer> idList) {
        if (idList.isEmpty()) {
            return Result.fail(ErrorEnums.InfoIsEmpty.getLabel(), ErrorEnums.InfoIsEmpty.getValue());
        }
        int count = menusMapper.BatchDel(idList);
        if (count == 0) {
            return Result.fail(ErrorEnums.Deleteinfo_Fail.getLabel(), ErrorEnums.Deleteinfo_Fail.getValue());
        }
        return Result.ok("", ErrorEnums.Success.getValue());
    }

    @Override
    public Result queryMenuByType() {
        List<Menu> list = menusMapper.queryMenuByType();
        return Result.ok("", list, ErrorEnums.Success.getValue());
    }

    @Override
    public Result updateMenuStatus(Menu menu) {
        if (menu == null) {
            return Result.fail(ErrorEnums.InfoIsEmpty.getLabel(), ErrorEnums.InfoIsEmpty.getValue());
        }
        return getResult(menu);
    }

    private Result getResult(Menu menu) {
        if (menu.getSuperMenu() != null && menu.getSuperMenu().equals("")) {
            menu.setSuperMenu(null);
        }
        int count = menusMapper.updateMenus(menu);
        if (count == 0) {
            return Result.fail(ErrorEnums.Info_Fail.getLabel(), ErrorEnums.Info_Fail.getValue());
        }
        return Result.ok("", ErrorEnums.Success.getValue());
    }


}
